package com.huawei.rbac.result;

import lombok.Getter;
import lombok.ToString;

import java.util.Collections;
import java.util.List;

/**
 * 分页结果封装
 *
 * @author luohua
 */
@Getter
@ToString
public class PageResult<T> {

    private final List<T> data;
    private final int currentPage;
    private final int pageSize;
    private final int totalCount;

    private final int totalPage;
    private final int prevPage;
    private final int nextPage;

    public PageResult(List<T> data, int currentPage, int pageSize, int totalCount) {
        this.data = data;
        this.currentPage = currentPage;
        this.pageSize = pageSize;
        this.totalCount = totalCount;

        this.totalPage = this.totalCount % this.pageSize == 0 ? this.totalCount / this.pageSize : this.totalCount / this.pageSize + 1;
        this.prevPage = Math.max(this.currentPage - 1, 1);
        this.nextPage = Math.min(this.currentPage + 1, this.totalPage);
    }

    public PageResult(int currentPage, int pageSize) {
        this(Collections.emptyList(), currentPage, pageSize, 0);
    }
}
